home *** CD-ROM | disk | FTP | other *** search
/ ADA Programming Guide / ADA Programming Guide.iso / ada_gnu / adainc / i-cstrin.adb < prev    next >
Text File  |  1996-01-30  |  8KB  |  277 lines

  1. ------------------------------------------------------------------------------
  2. --                                                                          --
  3. --                         GNAT COMPILER COMPONENTS                         --
  4. --                                                                          --
  5. --                 I N T E R F A C E S . C . S T R I N G S                  --
  6. --                                                                          --
  7. --                                 B o d y                                  --
  8. --                                                                          --
  9. --                            $Revision: 1.9 $                              --
  10. --                                                                          --
  11. --           Copyright (c) 1992,1993,1994 NYU, All Rights Reserved          --
  12. --                                                                          --
  13. -- The GNAT library is free software; you can redistribute it and/or modify --
  14. -- it under terms of the GNU Library General Public License as published by --
  15. -- the Free Software  Foundation; either version 2, or (at your option) any --
  16. -- later version.  The GNAT library is distributed in the hope that it will --
  17. -- be useful, but WITHOUT ANY WARRANTY;  without even  the implied warranty --
  18. -- of MERCHANTABILITY  or  FITNESS FOR  A PARTICULAR PURPOSE.  See the  GNU --
  19. -- Library  General  Public  License for  more  details.  You  should  have --
  20. -- received  a copy of the GNU  Library  General Public License  along with --
  21. -- the GNAT library;  see the file  COPYING.LIB.  If not, write to the Free --
  22. -- Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.        --
  23. --                                                                          --
  24. ------------------------------------------------------------------------------
  25.  
  26. with System; use System;
  27. with System.Address_To_Access_Conversions;
  28.  
  29. package body Interfaces.C.Strings is
  30.  
  31.    package Char_Access is new Address_To_Access_Conversions (char);
  32.  
  33.    -----------------------
  34.    -- Local Subprograms --
  35.    -----------------------
  36.  
  37.    function Peek (From : chars_ptr) return char;
  38.    pragma Inline (Peek);
  39.    --  Given a chars_ptr value, obtain referenced character
  40.  
  41.    procedure Poke (Value : char; Into : chars_ptr);
  42.    pragma Inline (Poke);
  43.    --  Given a chars_ptr, modify referenced Character value
  44.  
  45.    function "+" (Left : chars_ptr; Right : size_t) return chars_ptr;
  46.    pragma Inline ("+");
  47.    --  Address arithmetic on chars_ptr value
  48.  
  49.    function Position_Of_Nul (Into : char_array) return size_t;
  50.    --  Returns position of the first Nul in Into or Into'Last + 1 if none
  51.  
  52.    function C_Malloc (Size : size_t) return chars_ptr;
  53.    pragma Import (C, C_Malloc, "malloc");
  54.  
  55.    procedure C_Free (Address : chars_ptr);
  56.    pragma Import (C, C_Free, "free");
  57.  
  58.    ---------
  59.    -- "+" --
  60.    ---------
  61.  
  62.    function "+" (Left : chars_ptr; Right : size_t) return chars_ptr is
  63.    begin
  64.       return Left + chars_ptr (Right);
  65.    end "+";
  66.  
  67.    ----------
  68.    -- Free --
  69.    ----------
  70.  
  71.    procedure Free (Item : in out chars_ptr) is
  72.    begin
  73.       if Item = Null_Ptr then
  74.          return;
  75.       end if;
  76.  
  77.       C_Free (Item);
  78.       Item := Null_Ptr;
  79.    end Free;
  80.  
  81.    --------------------
  82.    -- New_Char_Array --
  83.    --------------------
  84.  
  85.    function New_Char_Array (Chars : in char_array) return chars_ptr is
  86.       Index   : size_t;
  87.       Pointer : chars_ptr;
  88.  
  89.    begin
  90.       --  Get index of position before null. This can be -1, which is OK!
  91.  
  92.       Index := Position_Of_Nul (Into => Chars) - 1;
  93.  
  94.       --  Returned value is length of signficant part + 1 for the nul character
  95.  
  96.       Pointer := C_Malloc ((Index - Chars'First + 1) + 1);
  97.       Update (Item   => Pointer,
  98.               Offset => 0,
  99.               Chars  => Chars,
  100.               Check  => False);
  101.       return Pointer;
  102.    end New_Char_Array;
  103.  
  104.    ----------------
  105.    -- New_String --
  106.    ----------------
  107.  
  108.    function New_String (Str : in String) return chars_ptr is
  109.    begin
  110.       return New_Char_Array (To_C (Str));
  111.    end New_String;
  112.  
  113.    ----------
  114.    -- Peek --
  115.    ----------
  116.  
  117.    function Peek (From : chars_ptr) return char is
  118.       use Char_Access;
  119.    begin
  120.       return To_Pointer (Address (To_Address (From))).all;
  121.    end Peek;
  122.  
  123.    ----------
  124.    -- Poke --
  125.    ----------
  126.  
  127.    procedure Poke (Value : char; Into : chars_ptr) is
  128.       use Char_Access;
  129.    begin
  130.       To_Pointer (Address (To_Address (Into))).all := Value;
  131.    end Poke;
  132.  
  133.    ---------------------
  134.    -- Position_Of_Nul --
  135.    ---------------------
  136.  
  137.    function Position_Of_Nul (Into : char_array) return size_t is
  138.    begin
  139.       for J in Into'Range loop
  140.          if Into (J) = nul then
  141.             return J;
  142.          end if;
  143.       end loop;
  144.  
  145.       return Into'Last + 1;
  146.    end Position_Of_Nul;
  147.  
  148.    ------------
  149.    -- Strlen --
  150.    ------------
  151.  
  152.    function Strlen (Item : in chars_ptr) return size_t is
  153.       Item_Index : size_t := 0;
  154.  
  155.    begin
  156.       if Item = Null_Ptr then
  157.          raise Dereference_Error;
  158.       end if;
  159.  
  160.       loop
  161.          if Peek (Item + Item_Index) = nul then
  162.             return Item_Index;
  163.          end if;
  164.  
  165.          Item_Index := Item_Index + 1;
  166.       end loop;
  167.    end Strlen;
  168.  
  169.    ------------------
  170.    -- To_Chars_Ptr --
  171.    ------------------
  172.  
  173.    function To_Chars_Ptr
  174.      (Item      : in char_array_access;
  175.       Nul_Check : in Boolean := False)
  176.       return      chars_ptr
  177.    is
  178.    begin
  179.       if Item = null then
  180.          return Null_Ptr;
  181.       elsif Nul_Check
  182.         and then Position_Of_Nul (Into => Item.all) > Item'Last
  183.       then
  184.          raise Terminator_Error;
  185.       else
  186.          return To_Integer (Item (Item'First)'Address);
  187.       end if;
  188.    end To_Chars_Ptr;
  189.  
  190.    ------------
  191.    -- Update --
  192.    ------------
  193.  
  194.    procedure Update
  195.      (Item   : in chars_ptr;
  196.       Offset : in size_t;
  197.       Chars  : in char_array;
  198.       Check  : Boolean := True)
  199.    is
  200.       Index : chars_ptr := Item + Offset;
  201.  
  202.    begin
  203.       if Check and then Offset + Chars'Length  > Strlen (Item) then
  204.          raise Update_Error;
  205.       end if;
  206.  
  207.       for J in Chars'Range loop
  208.          Poke (Chars (J), Into => Index);
  209.          Index := Index + 1;
  210.       end loop;
  211.    end Update;
  212.  
  213.    procedure Update
  214.      (Item   : in chars_ptr;
  215.       Offset : in size_t;
  216.       Str    : in String;
  217.       Check  : in Boolean := True)
  218.    is
  219.    begin
  220.       Update (Item, Offset, To_C (Str), Check);
  221.    end Update;
  222.  
  223.    -----------
  224.    -- Value --
  225.    -----------
  226.  
  227.    function Value (Item : in chars_ptr) return char_array is
  228.       Result : char_array (0 .. Strlen (Item));
  229.  
  230.    begin
  231.       if Item = Null_Ptr then
  232.          raise Dereference_Error;
  233.       end if;
  234.  
  235.       --  Note that the following loop will also copy the terminating Nul
  236.  
  237.       for J in Result'Range loop
  238.          Result (J) := Peek (Item + J);
  239.       end loop;
  240.  
  241.       return Result;
  242.    end Value;
  243.  
  244.    function Value
  245.      (Item   : in chars_ptr;
  246.       Length : in size_t)
  247.       return   char_array
  248.    is
  249.       Result : char_array (0 .. Length - 1);
  250.  
  251.    begin
  252.       if Item = Null_Ptr then
  253.          raise Dereference_Error;
  254.       end if;
  255.  
  256.       for J in Result'Range loop
  257.          Result (J) := Peek (Item + J);
  258.          if Result (J) = nul then
  259.             return Result (0 .. J);
  260.          end if;
  261.       end loop;
  262.  
  263.       return Result;
  264.    end Value;
  265.  
  266.    function Value (Item : in chars_ptr) return String is
  267.    begin
  268.       return To_Ada (Value (Item));
  269.    end Value;
  270.  
  271.    function Value (Item : in chars_ptr; Length : in size_t) return String is
  272.    begin
  273.       return To_Ada (Value (Item, Length));
  274.    end Value;
  275.  
  276. end Interfaces.C.Strings;
  277.